package com.core.website.service.impl;

import com.core.website.entity.Column;
import com.core.website.mapper.ColumnMapper;
import com.core.website.service.ColumnService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class ColumnServiceImpl implements ColumnService {
    @Autowired
    private ColumnMapper columnMapper;
    @Override
    @CacheEvict(value="column",allEntries = true)
    public int add(Column column) {
        column.setId(UUID.randomUUID().toString().toUpperCase().replace("-", ""));
        if(StringUtils.isEmpty(column.getParentId())){
            column.setInnerCode(column.getId());
        }else{
            Column column1 = columnMapper.queryById(column.getParentId());
            column.setInnerCode(column1.getInnerCode()+","+column.getId());
        }
        return columnMapper.add(column);
    }

    @Override
    @CacheEvict(value="column",allEntries = true)
    public int edit(Column column) {
        return columnMapper.edit(column);
    }

    @Override
    @CacheEvict(value="column",allEntries = true)
    public int delete(List<String> ids) {
        return columnMapper.delete(ids);
    }

    private void buildInnerName(List<Column> result){
        Map<String, List<Column>> collect = result.stream().collect(Collectors.groupingBy(x -> x.getId()));
        for(Column column:result){
            String innerCode = column.getInnerCode();
            if(StringUtils.isNotEmpty(innerCode)){
                String[] split = innerCode.split(",");
                StringBuilder innerName=new StringBuilder();
                for(String id:split){
                    if(null==collect.get(id)){
                        Column column1 = columnMapper.queryById(id);
                        String name=column1.getName();
                        if(innerName.length()==0){
                            innerName.append(name);
                        }else{
                            innerName.append(",").append(name);
                        }
                    }else{
                        String name=collect.get(id).get(0).getName();
                        if(innerName.length()==0){
                            innerName.append(name);
                        }else{
                            innerName.append(",").append(name);
                        }
                    }

                }
                column.setInnerName(innerName.toString());
            }
        }
    }
    @Override
    @Cacheable(value = "column",keyGenerator = "selfKeyGenerate")
    public List<Column> queryAll(Column column) {
        List<Column> result=columnMapper.queryAll(column);
        if(null!=result&&result.size()>0){
            buildInnerName(result);
        }
        return result;
    }

    @Override
    @Cacheable(value = "column",key = "queryTree")
    public List<Column> queryTree() {
        List<Column> result=columnMapper.queryAll(new Column());
        if(null!=result&&result.size()>0){
            buildInnerName(result);
        }

        return result;
    }

    @Override
    @Cacheable(value = "column",key = "#id")
    public Column queryOne(String id) {
        List<Column> list=columnMapper.queryAll(new Column());
        Map<String, List<Column>> collect = list.stream().collect(Collectors.groupingBy(x -> x.getId()));

        Column column=columnMapper.queryById(id);
        String innerCode = column.getInnerCode();
        if(StringUtils.isNotEmpty(innerCode)){
            String[] split = innerCode.split(",");
            StringBuilder innerName=new StringBuilder();
            for(String columnId:split){
                String name=collect.get(columnId).get(0).getName();
                if(innerName.length()==0){
                    innerName.append(name);
                }else{
                    innerName.append(",").append(name);
                }
            }
            column.setInnerName(innerName.toString());
        }
        return column;
    }

    @Override
    public List<Column> queryByOrgId(String organizationId) {
        return columnMapper.queryByOrgId(organizationId);
    }
}
